package freespiritsocial.io.user.viewmodel

import freespiritsocial.io.user.data.model.bean.ApiRefreshResult
import freespiritsocial.io.user.data.model.bean.NotificationPojo
import freespiritsocial.io.user.data.model.bean.PostPojo
import freespiritsocial.io.user.network.apiService
import freespiritsocial.io.user.viewmodel.delegates.IPeopleApi
import freespiritsocial.io.user.viewmodel.delegates.PeopleDelegate
import freespiritsocial.io.user.viewmodel.delegates.UserInfoViewModel
import me.hgj.jetpackmvvm.callback.livedata.UnPeekLiveData
import me.hgj.jetpackmvvm.ext.request

class NotificationsViewModel: UserInfoViewModel(),IPeopleApi by PeopleDelegate() {
    val weekResult = UnPeekLiveData<ApiRefreshResult<NotificationPojo>>()
    val monthResult = UnPeekLiveData<ApiRefreshResult<NotificationPojo>>()
    fun getPushInfoList(lastPushId: Long? = null) {
        request({
            val pushInfoList = apiService.getPushInfoList(lastPushId)
            apiService.pushRead(pushInfoList.data.map { it.pushId }.joinToString(","))
            pushInfoList
        },{ list ->
            val weekList = list.filter { it.timeGroup?.contains("week",true) == true }
            val monthList = list.filter { it.timeGroup?.contains("month",true) == true }
            val isRefresh = lastPushId == null
            if (isRefresh && weekList.isNotEmpty()) {
                weekResult.value = (ApiRefreshResult(isRefresh,weekList,list.isNotEmpty()))
            }
            monthResult.value = (ApiRefreshResult(lastPushId == null,monthList,list.isNotEmpty()))
        })
    }

    fun clearAllPush(callback: () -> Unit) {
        request({
            apiService.clearAllPush()
        },{
            callback()
        })
    }

    fun setNotificationSetting(commentsOnOff: Int? = null,likeOnOff: Int? = null,storiesOnOff: Int? = null) {
        request({
            apiService.setNotificationSetting(
                hashMapOf("commentsOnOff" to commentsOnOff,"likeOnOff" to likeOnOff,"storiesOnOff" to storiesOnOff)
            )
        },{
        })
    }

    fun getPost(id: Long?,callback: (PostPojo) -> Unit) {
        request({
            apiService.getInvitationDetail(id)
        },{
            callback.invoke(it)
        },isShowDialog = true)
    }
}